package com.smdk.dsminio.controller;
import cn.hutool.log.StaticLog;
import com.smdk.dsminio.apiservice.FileInfoService;
import com.smdk.dsminio.service.FileStorageService;
import com.smdk.dsminio.utils.AjaxResult;
import com.smdk.dsminio.utils.ConsistentHashingWithoutVirtualNode;
import com.smdk.dsminio.vo.DsminioFileInfo;
import com.smdk.dsminio.vo.MultipartFileParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Date;

/**
 * 默认控制层
 * Created by 神秘的凯 on 22020/11/11.
 * version 1.0
 */
@RestController
@RequestMapping(value = "/api")
public class FileDiskStorageController {
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private FileInfoService dsminioFileInfoService;
    @Value("${spring.application.name}")
    private String applicationServerName;
    /**
     * 上传文件
     *
     * @param multipartFileParam
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadFileInfo", method = RequestMethod.POST)
    public AjaxResult uploadFileInfo(MultipartFileParam multipartFileParam) {
        StaticLog.info("上传文件start。");
        try {
           boolean uploadResult= fileStorageService.uploadFileByMappedByteBuffer(multipartFileParam);
           if (uploadResult){
               DsminioFileInfo dsminioFileInfo = new DsminioFileInfo();
               //buckedId
               dsminioFileInfo.setBucketId(multipartFileParam.getBucketId());
               //文件名称
               dsminioFileInfo.setFileName(multipartFileParam.getFileName());
               //文件状态
               dsminioFileInfo.setFileStatus(0);
               //服务器节点标识
               dsminioFileInfo.setNodeServerId(applicationServerName);
               //文件MD5
               dsminioFileInfo.setMd5(multipartFileParam.getMd5());
               //文件大小
               dsminioFileInfo.setSize(multipartFileParam.getSize());
               //文件后缀
               String suffix = dsminioFileInfo.getFileName().substring(dsminioFileInfo.getFileName().lastIndexOf(".") + 1);
               dsminioFileInfo.setSuffix(suffix);
               //文件夹ID
               dsminioFileInfo.setFolderId(multipartFileParam.getParentFolderId());
               //创建日期
               dsminioFileInfo.setCreateDateTime(new Date());
               //更新日期
               dsminioFileInfo.setUpdateTime(new Date());
               //文件路径
               String filePath= ConsistentHashingWithoutVirtualNode.getServer(multipartFileParam.getMd5())+dsminioFileInfo.getFileName();
               //计算path
               dsminioFileInfo.setFilePath(filePath.replace("&&", "/"));
               //调用Feign更新
               dsminioFileInfoService.insertDsminioFileInfo(dsminioFileInfo);
               StaticLog.info(dsminioFileInfo.getFilePath());
               return AjaxResult.success(uploadResult,"上传完毕");
           }else {
               return AjaxResult.fail(uploadResult,"分片上传中...正在上传第"+multipartFileParam.getChunk()+"块文件块，剩余"+(multipartFileParam.getChunks()-multipartFileParam.getChunk())+"个分块");
           }
        } catch (IOException e) {
            e.printStackTrace();
            StaticLog.error("文件上传失败。{}", multipartFileParam.toString());
            return AjaxResult.fail(false,"文件上传失败");
        }
    }
}
